<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreCountry extends Base {
	
	//constructor
	function CoreCountry() {
		parent::Base();
	}
	
	//methods
	function getCountries() {
		return $this->_get_countries();
	}
	
	function getCountriesFormat() {
		$countries = $this->_get_countries('', false);
		for($i=0, $n=sizeof($countries); $i<$n; $i++) {
			$countries_format[$countries[$i]['id']] = $countries[$i]['name'];
		}
		
		return $countries_format;
	}
	
	function getCountry($countries_id) {
		return $this->_get_countries($countries_id, false);
	}
	
	function getCountryA($countries_id) {
		return $this->adb->getRow("select * from pp_core_countries where countries_id = '" . (int)$countries_id . "'");
	}
	
	function getCountryISO($countries_id) {
		return $this->_get_countries($countries_id, true);
	}
	
	function getCountryName($countries_id) {
		$country = $this->_get_countries($countries_id, false);
		
		return $country['name'];
	}
	
	function getCountryShipping($countries_id) {
		$country = $this->getCountryISO($countries_id);
		$country = array('id' => $country['id'], 'title' => $country['name'], 'iso_code_2' => $country['iso_code_2'], 'iso_code_3' => $country['iso_code_3']);
		
		return $country;
	}
	
	function getCountryShippingAddress($countries_id) {
		$country = $this->getCountryISO($countries_id);
		$address['country'] = array('id' => $country['id'], 'title' => $country['name'], 'iso_code_2' => $country['iso_code_2'], 'iso_code_3' => $country['iso_code_3']);
		$address['country_iso2'] = $country['iso_code_2'];
		$address['country_iso3'] = $country['iso_code_3'];
		
		return $address;
	}
	
	//private
	function _get_countries($countries_id = '', $with_iso_codes = false) {
		$countries_array = array();
		
		if( $countries_id == '' ) {
			$query = "select countries_id, countries_name from pp_core_countries order by countries_name";
			$result = $this->adb->query($query);
			while($row = $this->adb->fetch_array($result)) {
				$countries_array[] = array(
									'id' => $row['countries_id'],
									'name' => $row['countries_name'],
										);
			}
		} else {
			if( $with_iso_codes == true ) {
				$query = "select countries_id, countries_name, countries_iso_code_2, countries_iso_code_3 from pp_core_countries " 
						. " where countries_id = '" . (int)$countries_id . "'";
				
				$result = $this->adb->query($query);
				$row = $this->adb->fetch_array($result);
				
				$countries_array = array(
								'id' => $row['countries_id'],
								'name' => $row['countries_name'],
								'iso_code_2' => $row['countries_iso_code_2'],
								'iso_code_3' => $row['countries_iso_code_3'],
									);
			} else {
				$query = "select countries_id, countries_name from pp_core_countries where countries_id = '" . (int)$countries_id . "'";
				
				$result = $this->adb->query($query);
				$row = $this->adb->fetch_array($result);
				
				$countries_array = array(
								'id' => $row['countries_id'],
								'name' => $row['countries_name'],
									);
			}
		}
		
		return $countries_array;
	}
}

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class CoreCountryBase extends BaseItemBase {
	
	//constructor
	function CoreCountryBase() {
		parent::BaseItemBase();
		
		$this->table = 'pp_core_countries';
		$this->prefix = 'countries_';
		$this->_table_owner_usable = false;
		$this->item_fields = array('id', 'name', 'code' => 'iso_code_2', 'code2' => 'iso_code_2', 'code3' => 'iso_code_3');
	}
	
	//methods
	function getCountriesFormat() {
		$countries = $this->getItems('', 0, false);
		for($i=0, $n=sizeof($countries); $i<$n; $i++) {
			$countries_format[$countries[$i]['id']] = $countries[$i]['name'];
		}
		
		return $countries_format;
	}
	
	function getFormat($filter = '', $check = 0, $limit = true) {
		return $this->getItemsFormat($filter, $check, $limit, array('id', 'name'));
	}
	
	function getFormatList($filter = '', $check = 0, $limit = true) {
		$format = $this->format;
		$format_check = $this->format_check;
		$this->format = true;
		$this->format_check = false;
		
		$items = $this->getItems($filter, $check, $limit);
		$this->format = $format;
		$this->format_check = $format_check;
		
		return $items;
	}
}
//
?>